var Quest = GameEntity.extend({
	init: function(id, config){
		this._super(id, config);
		this.requirements = config.requirements;
		this.isTaken = false;
		this.isComplete = false;
		this.inRange = false;
		this.isOpen = false;
		this.isReturned = false;
		this.item = config.item;
		this.i = config.i;
		this.j = config.j;
		this.frame = 0;
		this.frames = 4;
		this.frameWidth = this.frameHeight = 55;
		this.token = '!'
		this.takeText = questSpecs[id].takeText;
		
		this.giveText = questSpecs[id].giveText;
		
		this.cx = this.x + 17;
		this.cy = this.y + 17;
		this.cr = this.x + 40;
		this.cb = this.y + 50;
		this.depth = 2;
	},
	draw: function(canvas){
		canvas.ctx.drawImage(this.sprite.image, 0, this.h*this.id, this.h, this.w, this.x, this.y, this.h, this.w);
		canvas.ctx.save();
		canvas.ctx.font = "30px disposabledroid";
		canvas.ctx.textAlign = 'center';
		canvas.ctx.fillStyle = '#ffffff';
		canvas.ctx.shadowColor = '#444444'
		canvas.ctx.shadowOffsetX = 2;
		canvas.ctx.shadowOffsetY = 2;
		canvas.ctx.fillText(this.token, this.x + this.w/2, this.y);
		canvas.ctx.restore();
		canvas.ctx.drawImage(this.sprite.image, this.w+Math.floor(this.frame)*this.frameWidth, 0+this.id*this.h, this.frameWidth , this.frameHeight , this.x+this.frameWidth , this.y, this.frameWidth, this.frameHeight);
		if(this.frame >= this.frames){
			this.frame = 0;
		}else{
			this.frame += 0.4;
		}
	},
	manage: function(infoBar, canvas){
		if(this.isReturned ){
			return;
		}
			
		if(!this.isOpen){
			infoBar.setMessage('Press \'i\' to open the quest', canvas, 48);
		} 
		this.inRange = true;

	},
	
	update: function(keysPress, hero, canvas, infoBar, map, bgObjects){
	if(this.isReturned || this.i !== map.i || this.j !== map.j){
		return;
	}
	
	if(hero.questItems[this.id] >= this.requirements){
		this.isComplete = true;
		this.token = '?';
	}
	if (this.inRange && (105 in keysPress || 73 in keysPress)){
		this.isOpen = !this.isOpen;
	}
	
	if(this.isOpen){
		//ако куеста не е взет
		if(!this.isTaken){
			if (111 in keysPress || 79 in keysPress){
				this.isTaken = true;
				this.token = '*'
				hero.takenQuests[this.id] = true;
				hero.questItems[this.id] = 0;
				this.isOpen = false;
			}
		}
		//ако куеста e завършен
		if(this.isComplete){
			if (112 in keysPress || 80 in keysPress){
				this.isReturned = true;
				this.token = 'x';
				infoBar.setMessage('Congratulations, you finished the quest!', canvas);
				this.isOpen = false;
				hero.takenQuests[this.id] = false;
				hero.returnedQuests[this.id] = true;
				this.drop(map, bgObjects);
				
			}
		}
	}
	
	delete keysPress[73];	
	delete keysPress[105];
	delete keysPress[79];	
	delete keysPress[111];
	delete keysPress[80];	
	delete keysPress[112];
	
	},
	drawOpenedQuest: function(canvas){
		if (this.isOpen){
			var words = this.takeText.split(' ');
			var confirmMessage = 'To take the quest press \'o\'';
			var line = '';
			var w = canvas.height/2;
			var h = canvas.height/2;
			var x = (this.x > canvas.width/2) ? this.x - w - 10 : this.x + this.w + 10;
			var y = canvas.height*0.166;
			var lineHeigth = 15;
			if(this.isTaken && this.isComplete){
				words = this.giveText.split(' ');
				confirmMessage = 'To return the quest press \'p\''
			}
			
			canvas.ctx.save();
			canvas.ctx.fillStyle = '#b7a457';
			canvas.ctx.fillRect(x, y, w, h);
			canvas.ctx.font = "18px disposabledroid";
			canvas.ctx.fillStyle = '#ffffff';
			y += lineHeigth*2;
			x += lineHeigth;
			
			for (var i = 0; i < words.length; i++){
				var testLine = line + words[i] + " ";
				wordWidth = canvas.ctx.measureText(testLine).width;
				if (wordWidth > w - lineHeigth){
					canvas.ctx.fillText(line, x, y);
		            line = words[i] + " ";
		            y += lineHeigth;
				}else {
		            line = testLine;
		        }
			}
			canvas.ctx.fillText(line, x, y);
			y += lineHeigth*2;
			canvas.ctx.fillText(confirmMessage, x, y)
			//canvas.ctx.fillText(text, x + 10, y + 10);
			canvas.ctx.restore();
		}
	},
	drop: function (map, bgObjects){
		var item = new BonusObjects('q'+this.id,{x: this.x, y: this.cb, type: this.item });
	//	console.log(bgObjects);
		bgObjects[item.id] = item; 
		
	}
});